package com.xyhuo.leetcode.december;

/**
 * 移除元素
 * https://leetcode.cn/problems/remove-element/
 *
 * @time 2023/12/12
 */
public class RemoveElement {

//    public static void main(String[] args) {
//        RemoveElement re = new RemoveElement();
//        int[] nums = {0, 1, 2, 2, 3, 0, 4, 2};
//        re.removeElement(nums, 2);
//    }

    public int removeElement(int[] nums, int val) {
        int len = nums.length;
        int left = 0, right = 0;
        while (right < len) {
            if (nums[right] == val) {
                while (right < len) {
                    if (nums[right] != val) {
                        break;
                    }
                    right++;
                }
                if (right >= len) {
                    break;
                }
                nums[left] = nums[right];
            } else {
                if (left != right) {
                    nums[left] = nums[right];
                }
            }
            left++;
            right++;
        }
        return left;
    }
}
